package qianfeng.com.meijiadaka;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import qianfeng.com.meijiadaka.fragment.DiscoverFragment;
import qianfeng.com.meijiadaka.fragment.HomeFragment;
import qianfeng.com.meijiadaka.fragment.MallFragment;
import qianfeng.com.meijiadaka.fragment.MyFragment;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.rg)
    RadioGroup mRg;
    @BindView(R.id.home_rb)
    RadioButton mHomeRb;
    @BindView(R.id.mall_rb)
    RadioButton mMallRb;
    @BindView(R.id.discover_rb)
    RadioButton mDiscoverRb;
    @BindView(R.id.my_rb)
    RadioButton mMyRb;

    private ArrayList<Fragment> mList;
    private FragmentManager mManager;
    private FragmentTransaction mTransaction;
    private HomeFragment mHomeFragment;
    private RadioButton[]mButtons;
    private MallFragment mMallFragment;
    private DiscoverFragment mDiscoverFragment;
    private MyFragment mMyFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        mList = new ArrayList<>();
        mManager = getSupportFragmentManager();
        mTransaction = mManager.beginTransaction();
        mHomeFragment = new HomeFragment();
        mTransaction.add(R.id.ll, mHomeFragment);
        mTransaction.commit();
        mList.add(mHomeFragment);
        mMallFragment = new MallFragment();
        mTransaction.add(R.id.ll, mMallFragment);
        mList.add(mMallFragment);
        mDiscoverFragment = new DiscoverFragment();
        mList.add(mDiscoverFragment);
        mTransaction.add(R.id.ll, mDiscoverFragment);
        mMyFragment = new MyFragment();
        mList.add(mMyFragment);
        mTransaction.add(R.id.ll, mMyFragment);
        mButtons=new RadioButton[]{mHomeRb,mMallRb,mDiscoverRb,mMyRb};
        mRg.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction transaction = mManager.beginTransaction();
        switch (checkedId) {
            case R.id.home_rb:
                show(0,transaction);
                setColor(0);
                break;
            case R.id.mall_rb:
                show(1,transaction);
                setColor(1);
                break;
            case R.id.discover_rb:
                show(2,transaction);
                setColor(2);
                break;
            case R.id.my_rb:
                show(3,transaction);
                setColor(3);
                break;
        }
        transaction.commit();
    }

    public void setColor(int dex){
        for (int i = 0; i < mButtons.length; i++) {
            if (dex == i) {
                mButtons[i].setTextColor(Color.RED);
            } else {
                mButtons[i].setTextColor(Color.GRAY);
            }
        }
    }
    public void show(int dex,FragmentTransaction transaction){
        for (int i = 0; i < mList.size(); i++) {
            if (dex == i) {
                transaction.show(mList.get(i));
            } else {
                transaction.hide(mList.get(i));
            }
        }
    }
}
